package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.common.exception.ziDyException;
import com.atguigu.yygh.hosp.mapper.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void deleteById(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);

        if(department != null){
            departmentRepository.deleteById(department.getId());
        }

    }

    @Override
    public String getDepName(String hoscode, String depcode) {

        Department byHoscodeAndDepcode = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);

        return byHoscodeAndDepcode.getDepname();
    }

    @Override
    public List<DepartmentVo> findAllDepartent(String hoscode) {

        List<DepartmentVo> reslut = new ArrayList<>();

        //先根据hoscode查询出所有得科室名称
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        List<Department> DepartList = departmentRepository.findAll(example);

        //查询出所有的科室以后进行分组
        Map<String, List<Department>> collect = DepartList.stream().collect(Collectors.groupingBy(Department::getBigcode));

        //遍历map
        for (Map.Entry<String, List<Department>> entry : collect.entrySet()){

            //获取到map中得每一个对象
            DepartmentVo departmentVo = new DepartmentVo();
            //将大科室放到VO对象中，因为return得是vo对象
            departmentVo.setDepcode(entry.getKey());

            List<Department> value = entry.getValue();
            departmentVo.setDepname(value.get(0).getBigname());

            List<DepartmentVo> list = new ArrayList<>();
            //循环遍历，将每个大科室中得小科室set进去
            for (Department department1 : value) {
                DepartmentVo departmentVo1 = new DepartmentVo();

                departmentVo1.setDepcode(department1.getDepcode());
                departmentVo1.setDepname(department1.getDepname());

                list.add(departmentVo1);
            }
            departmentVo.setChildren(list);

            reslut.add(departmentVo);
        }

        return reslut;

    }

    @Override
    public Page<Department> selectPage(Map<String, Object> paramMap) {
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        //非必填
        String depcode = (String)paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        //创建排序
        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");

        //创建分页查询
        Pageable pageable = PageRequest.of(page-1, limit, sort);

        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写

        //创建条件对象
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department,matcher);

        Page<Department> all = departmentRepository.findAll(example, pageable);

        return all;

    }

    @Override
    public void save(Map<String, Object> map) {

        String s = JSONObject.toJSONString(map);

        Department department = JSONObject.parseObject(s, Department.class);

        if(department == null){
            throw new ziDyException(20001,"数据传入错误");
        }

        //查询科室是否存在  医院编号 + 科室编号
        Department existsDepartment = departmentRepository.
                findByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());

        if(existsDepartment != null){
            department.setId(existsDepartment.getId());
            department.setCreateTime(existsDepartment.getCreateTime());
            department.setUpdateTime(new Date());

            departmentRepository.save(department);
        }else{
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());

            departmentRepository.save(department);
        }



    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {

        Department byHoscodeAndDepcode = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);

        return byHoscodeAndDepcode;
    }
}
